package com.hibase.core.rule.utils.operator;

import com.ql.util.express.ArraySwap;
import com.ql.util.express.InstructionSetContext;
import com.ql.util.express.OperateData;
import com.ql.util.express.exception.QLException;
import com.ql.util.express.instruction.OperateDataCacheManager;
import com.ql.util.express.instruction.op.OperatorBase;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 别名 size  长度
 */
@Slf4j
public class SizeOperator extends OperatorBase {

    @Override
    public OperateData executeInner(InstructionSetContext context, ArraySwap list) throws Exception {
        OperateData p0 = list.get(0);
        if(p0 == null || p0.getObject(context) == null){
            throw new QLException("对象为null,不能执行list相关操作");
        }
        Object tmpObject = p0.getObject(context);
        if(tmpObject instanceof List){
            int size = ((List) tmpObject).size();
            return OperateDataCacheManager.fetchOperateData(size,Integer.class);
        }
        throw new QLException("对象:"+ tmpObject.getClass() +"不是list,不能执行相关操作" );
    }
}
